﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CrazyBubbleToolkit
{
    /// <summary>
    /// Interaction logic for PropertyWindow.xaml
    /// </summary>
    public partial class PropertyWindow : Window
    {
        private StageData StageData { get; set; }

        public PropertyWindow()
        {
            InitializeComponent();
        }


        #region 关卡属性显示

        /// <summary>
        /// 关卡属性显示
        /// </summary>
        public void ShowAsDialog(StageData sd)
        {
            StageData = sd;

            stageTypeComboBox.SelectedIndex = StageData.GridsType;
            GoalTB.Text = StageData.Goal.ToString();

            ratio0TextBox.Text = StageData.BulletRedRatio.ToString();
            ratio1TextBox.Text = StageData.BulletYellowRatio.ToString();
            ratio2TextBox.Text = StageData.BulletBlueRatio.ToString();
            ratio3TextBox.Text = StageData.BulletGreenRatio.ToString();
            ratio4TextBox.Text = StageData.BulletPurpleRatio.ToString();

            textBoxMoveSpeed.Text = StageData.moveSpeed.ToString();         //下落速度

            //设置距离速度属性值到对应控件
            TextBox[] text1 = { TextBoxDistance1, TextBoxDistance2, TextBoxDistance3, TextBoxDistance4, TextBoxDistance5 };
            TextBox[] text2 = { TextBoxSpeed1, TextBoxSpeed2, TextBoxSpeed3, TextBoxSpeed4, TextBoxSpeed5 }; 
            
            DistanceSpeed Elem;
            for (int i = 0; i < StageData.distanceSpeed.Count; i++)
            {
                Elem = StageData.distanceSpeed.ElementAt(i);
                text1[i].Text = ((float)(Elem.d / 10f)).ToString();
                text2[i].Text = ((float)(Elem.s)).ToString();
            }

            //设置控制开关状态
            //switch1.IsChecked = (StageData.switch1 == 1);
            TexBox_switch1.Text = StageData.switch1.ToString();
            TexBox_switch2.Text = StageData.switch2.ToString();
            TexBox_switch3.Text = StageData.switch3.ToString();
            TexBox_switch4.Text = StageData.switch4.ToString();
            TexBox_switch5.Text = StageData.switch5.ToString();

            bulletsCountTextBox.Text = StageData.TotalBullets.ToString();

            star1TextBox.Text = StageData.Star1.ToString();
            star2TextBox.Text = StageData.Star2.ToString();
            star3TextBox.Text = StageData.Star3.ToString();

            ShowDialog();
        }
        #endregion


        #region 关卡属性保存

        /// <summary>
        /// 关卡属性保存
        /// </summary>
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            int type = 0;
            if (stageTypeComboBox.SelectedIndex != -1)
                type = stageTypeComboBox.SelectedIndex;
            StageData.GridsType = type;
            StageData.Goal = int.Parse(GoalTB.Text);

            StageData.BulletRedRatio = (int)float.Parse(ratio0TextBox.Text);
            StageData.BulletYellowRatio = (int)float.Parse(ratio1TextBox.Text);
            StageData.BulletBlueRatio = (int)float.Parse(ratio2TextBox.Text);
            StageData.BulletGreenRatio = (int)float.Parse(ratio3TextBox.Text);
            StageData.BulletPurpleRatio = (int)float.Parse(ratio4TextBox.Text);

            StageData.moveSpeed = Int32.Parse(textBoxMoveSpeed.Text.Trim());        //下落速度

            //保存距离速度数据
            StageData.distanceSpeed.Clear();
            TextBox[] text1 = { TextBoxDistance1, TextBoxDistance2, TextBoxDistance3, TextBoxDistance4, TextBoxDistance5 };
            TextBox[] text2 = { TextBoxSpeed1, TextBoxSpeed2, TextBoxSpeed3, TextBoxSpeed4, TextBoxSpeed5 };
            float num1 = 0, num2 = 0;
            for (int i = 0; i < text1.Length; i++ )
            {
                try {  num1 = float.Parse(text1[i].Text); }
                catch (Exception) { num1 = 0; }

                try { num2 = float.Parse(text2[i].Text); }
                catch (Exception) { num2 = 0; }

                if (num1 != 0 || num2 != 0)
                {
                    DistanceSpeed Elem = new DistanceSpeed
                    {
                        d = (int)(num1 * 10),
                        s = (int)(num2)
                    };
                    StageData.distanceSpeed.Add(Elem);
                }
            }

            //保存控制开关状态
            //StageData.switch1 = (switch1.IsChecked == true ? 1 : 0);
            StageData.switch1 = int.Parse(TexBox_switch1.Text);
            StageData.switch2 = int.Parse(TexBox_switch2.Text);
            StageData.switch3 = int.Parse(TexBox_switch3.Text);
            StageData.switch4 = int.Parse(TexBox_switch4.Text);
            StageData.switch5 = int.Parse(TexBox_switch5.Text);
    
            StageData.TotalBullets = int.Parse(bulletsCountTextBox.Text);

            StageData.Star1 = int.Parse(star1TextBox.Text);
            StageData.Star2 = int.Parse(star2TextBox.Text);
            StageData.Star3 = int.Parse(star3TextBox.Text);

            Close();
        }

        #endregion


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }
    }
}
